﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mime;
using System.Text;
using System.Web;

namespace DataBaseBackUpUtil
{
    public class FMail
    {

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool SendMail(MailModel model)
        {
            try
            {
                System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage();
                //邮件主题
                mmsg.Subject = model.Title;
                mmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
                //邮件正文
                mmsg.Body = model.Content;
                //正文编码
                mmsg.BodyEncoding = Encoding.UTF8;
                //优先级
                mmsg.Priority = System.Web.Mail.MailPriority.High;

                System.Web.Mail.MailAttachment data = null;
                if (model.AttachFilePath != "")
                {
                    System.Web.Mail.MailAttachment attachment = new System.Web.Mail.MailAttachment(model.AttachFilePath); //create the attachment
                    mmsg.Attachments.Add(attachment); //add the attachment
                }
                //发件者邮箱地址
                mmsg.From = model.SenderAddress;

                //收件人收箱地址
                mmsg.To = model.ReceiverAddress;
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                //用户名
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", model.SenderAddress);
                //密码
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", model.SenderPassword);
                //端口
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
                //使用SSL
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                //Smtp服务器
                System.Web.Mail.SmtpMail.SmtpServer = "smtp.qq.com";
                System.Web.Mail.SmtpMail.Send(mmsg);
                return true;
            }
            catch (Exception e)
            {
                FLog.WriteLog("发送邮件失败，异常信息："+e.Message);
                return false;
            }
        }

        public struct MailModel
        {
            /// <summary>
            /// 收件人地址
            /// </summary>
            public string ReceiverAddress { get; set; }
            /// <summary>
            /// 收件人姓名
            /// </summary>
            public string ReceiverName { get; set; }
            /// <summary>
            /// 标题
            /// </summary>
            public string Title { get; set; }
            /// <summary>
            /// 内容
            /// </summary>
            public string Content { get; set; }
            /// <summary>
            /// 发件人地址（非必填）
            /// </summary>
            public string SenderAddress { get; set; }
            /// <summary>
            /// 发件人姓名（非必填）
            /// </summary>
            public string SenderName { get; set; }
            /// <summary>
            /// 发件人密码（非必填）
            /// </summary>
            public string SenderPassword { get; set; }
            /// <summary>
            /// 附件本地地址
            /// </summary>
            public string AttachFilePath { get; set; }
        }
    }

}